from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework import viewsets
from rest_framework.response import Response
from .serializers import StudentSerializer, ClassesSerializer
from .models import Student, Classes
from rest_framework.status import HTTP_404_NOT_FOUND,HTTP_204_NO_CONTENT

# Create your views here.
"""
    视图集
    list 查询全部 create 增加 retrieve 查询单条 update 修改 destroy 删除
"""
class StudentViewSet(viewsets.ModelViewSet):
    queryset = Student.objects.filter(is_delete=False)  # 定义视图集使用的查询集
    serializer_class = StudentSerializer  # 视图集使用的序列化器类


class ClassesViewSet(viewsets.ModelViewSet):
    queryset = Classes.objects.filter(is_delete=False)
    serializer_class = ClassesSerializer

    @action(methods=['get'], detail=False)
    def last(self, request):
        # 查询出班级的最后一条数据
        classes = Classes.objects.last()
        # 将查询出来的模型对象转化为json数据格式，然后进行返回
        return Response(self.get_serializer(classes).data)